package com.chuangke.security.userdetails;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.chuangke.admin.entity.SysUser;
import com.chuangke.admin.service.SysUserService;
import com.chuangke.common.exception.ChuangkeException;
import com.chuangke.security.GrantedAuthorityImpl;

/**
 * 用户登录认证信息查询
 * 
 * @author chuangke
 * @date Nov 20, 2018
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

	@Autowired
	private SysUserService sysUserService;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		SysUser user = sysUserService.findByName(username);
		if (user == null) {
			throw new ChuangkeException("登录失败，用户名或密码错误") ;
		}
		
//		// 账号锁定
		if (user.getStatus() == 0) {
			throw new ChuangkeException("账号已被锁定,请联系管理员");
		}

		// 用户权限列表，根据用户拥有的权限标识与如 @PreAuthorize("hasAuthority('sys:menu:view')")
		// 标注的接口对比，决定是否可以调用接口
		Set<String> permissions = sysUserService.findPermissions(user.getName());
		List<GrantedAuthority> grantedAuthorities = permissions.stream().map(GrantedAuthorityImpl::new)
				.collect(Collectors.toList());
		return new JwtUserDetails(user, grantedAuthorities);
	}
}